
#ifndef DOOR_COMMON_H
#define DOOR_COMMON_H

#include "gkt_board.h"
#include "gkt_debug.h"
//#include "gkt_wkio.h"
#include "gkt_wakeup.h"
#include "gkt_os.h"
#include "gkt_gpio.h"
#include "gkt_door.h"

#define DOOR_LOCK_MOTOR_DRVIC_UNKNOWN	0
#define DOOR_LOCK_MOTOR_DRVIC_ST18837	1
#define DOOR_LOCK_MOTOR_DRVIC_TMI8260	2

#ifdef GKT_CONFIG_DOOR_LOCK_MOTOR_DRVIC
#define DOOR_LOCK_MOTOR_DRVIC_L2(name)	\
	DOOR_LOCK_MOTOR_DRVIC_##name
#define DOOR_LOCK_MOTOR_DRVIC_L1(name)	\
	DOOR_LOCK_MOTOR_DRVIC_L2(name)
#define DOOR_LOCK_MOTOR_DRVIC	\
	DOOR_LOCK_MOTOR_DRVIC_L1(GKT_CONFIG_DOOR_LOCK_MOTOR_DRVIC)
#else
#error "not define motor drive IC."
#endif

enum {
	DOOR_LOCK_REQ_OPEN = 0,
	DOOR_LOCK_REQ_CLOSE,
#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC
	DOOR_LOCK_REQ_SLEF_LEARNING,
#endif
	DOOR_LOCK_REQ_MAX
};

int door_lock_init(void);
int door_lock_motor_init(void);
int door_lock_motor_rotate_forward(void);
int door_lock_motor_rotate_reverse(void);
void door_lock_motor_rotate_brake(void);
void door_lock_motor_rotate_stop(void);
void door_lock_motor_reset(void);
int door_lock_motor_standby(void);


#if defined(GKT_CONFIG_DOOR_MAGNETIC_ENABLE) && GKT_CONFIG_DOOR_MAGNETIC_ENABLE
int door_magnetic_init(void);
int door_magnetic_is_detected(void);
#endif


#if defined(GKT_CONFIG_DOOR_TAMPER_ENABLE) && GKT_CONFIG_DOOR_TAMPER_ENABLE
#ifdef GKT_CONFIG_DOOR_TAMPER_DETECT_LEVEL
#define DOOR_TAMPER_DETECT_LEVEL	\
	GKT_CONFIG_DOOR_TAMPER_DETECT_LEVEL
#else
#define DOOR_TAMPER_DETECT_LEVEL	\
	s_door_tamper_detect_level
#endif

#ifdef GKT_CONFIG_DOOR_TAMPER_WAKEUP_CHECK_TIME_US
#define DOOR_TAMPER_WAKEUP_CHECK_TIME_US	\
	GKT_CONFIG_DOOR_TAMPER_WAKEUP_CHECK_TIME_US
#else
#define DOOR_TAMPER_WAKEUP_CHECK_TIME_US	100
#endif
#define DOOR_TAMPER_WAKEUP_CHECK_UNIT_US	20
#define DOOR_TAMPER_WAKEUP_CHECK_TIMES	\
	(DOOR_TAMPER_WAKEUP_CHECK_TIME_US / DOOR_TAMPER_WAKEUP_CHECK_UNIT_US)

#define DOOR_TAMPER_IO_DEBOUNCE_READY	(1U << 0)
#define DOOR_TAMPER_IO_DEBOUNCE_BUSY	(1U << 1)

int door_tamper_init(void);
#endif

#if defined(GKT_CONFIG_DOOR_KEY_ENABLE) && GKT_CONFIG_DOOR_KEY_ENABLE
int door_key_init(void);
#endif

#if defined(GKT_CONFIG_DOOR_ADMIN_UNLOCK_ENABLE) && GKT_CONFIG_DOOR_ADMIN_UNLOCK_ENABLE
int door_admin_unlock_init(void);
#endif

void door_inner_notify(uint32_t type, uint32_t detected);

#endif	/* DOOR_COMMON_H */

